import numpy as np
from PIL.ImageChops import add, subtract, multiply, screen, difference
from matplotlib import font_manager
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt

fpath = 'C:/Windows/Fonts/simsun.ttc'
my_font = font_manager.FontProperties(fname=fpath)
def show(images_dict, subplot_size, wspace=None, hspace=None, font_properties=None):
    """
        对比显示多张图片
        :param images_dict: 图片对象字典
        :param subplot_size: 绘图子图矩阵尺寸
        :param wspace: 调整画布子图水平间距
        :param hspace: 调整画布子图竖直间距
        :param font_properties:字体属性
        """
    plt.subplots_adjust(wspace=wspace, hspace=hspace)
    for index, key in enumerate(images_dict, start=1):
        plt.subplot(*subplot_size, index)
        plt.title(key, fontproperties=font_properties)
        plt.imshow(images_dict[key])
    plt.show()
sakura_im = Image.open(fp="./../experiment_fig/sakura.jpg")  # (1600,1200)
bin_array = np.zeros_like(np.array(sakura_im))  # (1200, 1600, 3)
bin_array[500:901, 820:1300, :] = 255
bin_im = Image.fromarray(np.uint8(bin_array))
roi = multiply(sakura_im, bin_im)
images = {"sakura": sakura_im, "roi": roi}
show(images, (1, 2), wspace=0.5)

# plum_im = Image.open(fp="./../experiment_fig/plum_blossom.jpg")  # (1920,1200)
# resize_plum_im = plum_im.resize((sakura_im.width, sakura_im.height), Image.BICUBIC)



# diff_im = difference(sakura_im, resize_plum_im)
# subtract_im = subtract(sakura_im, resize_plum_im)

# add_im = add(sakura_im, resize_plum_im)
# multiply_im = multiply(sakura_im, resize_plum_im)
# screen_im = screen(sakura_im, resize_plum_im)

# alpha = 0.6  # out = image1 * (1.0 - alpha) + image2 * alpha
# blend_im = Image.blend(sakura_im, resize_plum_im, alpha=alpha)
# np_im = Image.fromarray(np.uint8(np.array(sakura_im) * (1 - alpha) + np.array(resize_plum_im) * alpha))

# crop_im = im.crop((400, 600, 800, 900))
# im.paste(crop_im, box=(0, 0))
# plt.title("图中图", fontproperties=my_font)
# plt.imshow(im)
# plt.show()

# plt.subplot(1, 2, 1)
# plt.title("原始图像", fontproperties=my_font)
# plt.imshow(im)
# im.thumbnail((im.width // 20, im.height // 20), resample=Image.BICUBIC)
# plt.subplot(1, 2, 2)
# plt.title("缩略图", fontproperties=my_font)
# plt.imshow(im)
# plt.show()

# show(images, (2, 1), font_properties=my_font)


# r, g, b = im.split()
# merge_im = Image.merge("RGB", (b, g, r))
# images = {"R": [r, plt.cm.Reds], "G": [g, plt.cm.Greens],
#           "B": [b, plt.cm.Blues], "merge image": [merge_im, None]}
#

# stat_img = Stat.Stat(im)
# print("""每个通道的最大值和最小值:{}
# 每个通道的像素数:{}
# 每个通道像素值的均值:{}
# 每个通道像素值中值:{}
# 每个通道像素值的标准差:{}""".format(stat_img.extrema, stat_img.count,
#                          stat_img.mean, stat_img.median, stat_img.stddev))


# draw_text(im, (700, 700), "Sakura", fpath, 100)


def draw_text(img, txt_coordinates, content, font_path, font_size=10):
    draw_shape = ImageDraw.Draw(img)
    font = ImageFont.truetype(font_path, size=font_size)
    draw_shape.text(xy=txt_coordinates, text=content, font=font)
    img.show()


def draw(img, draw_img, coordinates=None, width=None, outline=None):
    """
        实现在图片上绘制指定图形
        :param img: 原始图片
        :param draw_img: 绘制图形函数
        :param coordinates: 坐标(left,top,right,bottom)
        :param width: 线条宽度
        :param outline: 轮廓
        """
    draw_img(coordinates, outline, width=width)
    del draw_img
    img.show()


# draw(im, ImageDraw.Draw(im).ellipse, (500, 500, 800, 1000), outline="red")
# draw(im, ImageDraw.Draw(im).line, (500, 500, 1000, 1000), 6)
# draw(im, ImageDraw.Draw(im).rectangle, (500, 500, 1000, 1000), outline="blue")

# show(rotate_img(im, degrees), (2, 4), font_properties=my_font)


def rotate_img(img, degrees_ls):
    im_dict = {"原始图像": img}
    for degree in degrees_ls:
        im_dict["逆时针方向旋转" + str(degree) + chr(0x00B0)] = img.rotate(degree)
    return im_dict


degrees = [i * 45 for i in range(1, 8)]


def flip(img: Image, flip_dict):
    im_dict = {"原始图像": img}
    for method in flip_dict:
        im_dict[method] = img.transpose(flip_dict[method])
    return im_dict

# flip_methods = {"左右翻转": Image.FLIP_LEFT_RIGHT, "上下翻转": Image.FLIP_TOP_BOTTOM,
#                 "转置": Image.TRANSPOSE}
# cropped_img = im.crop(box=(300, 400, 600, 700))  # (left, upper, right, lower)
